package cn.kogee.men.controller;

import cn.kogee.basic.util.AjaxResult;
import cn.kogee.basic.util.PageList;
import cn.kogee.men.domain.Sstype;
import cn.kogee.men.query.SstypeQuery;
import cn.kogee.men.service.ISstypeService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 接口类：部门
 */
@RestController
@RequestMapping("/sstype")
@Api(value = "数据字典类型接口类",description = "数据字典类型的CRUD操作")
public class SstypeController {
    @Autowired
    private ISstypeService typeService;
    /**
     * 接口：查询所有
     * @return
     */
    // http://localhost:8080/sstype - Get
    @GetMapping
    @ApiOperation(value = "查询所有")
    public List<Sstype> findAll(){
        return typeService.findAll();
    }

    /**
     * 接口：查询单个对象
     * @return
     */
    // http://localhost:8080/sstype/2 - Get
    @GetMapping("/{id}")
    @ApiOperation(value = "查询单个对象")
    public Sstype findOne(@PathVariable("id") Long id){
        return typeService.findOne(id);
    }

    /**
     * 分页接口
     * @param
     * @return
     */
    // http://localhost:8080/sstype - Post - 参数通过json对象传递
    @PostMapping
    //@RequestBody -从请求体中获取数据赋值给departmentQuery
    @ApiOperation(value = "分页接口")
    public PageList<Sstype> queryPage(@RequestBody SstypeQuery typeQuery){
        return typeService.queryPage(typeQuery);
    }

    /**
     * 接口：删除
     * @param id
     * @return
     */
    // http://localhost:8080/sstype/2 - Delete
    @DeleteMapping("/{id}")
    @ApiOperation(value = "删除接口")
    public AjaxResult del(@PathVariable("id") Long id){
        try {
            typeService.del(id);
            return new AjaxResult();
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult(false,"删除失败");
//            return AjaxResult.me().setSuccess(false).setMsg("删除失败");
        }
    }

    /**
     * 接口：添加和修改
      * @param sstype
     * @return
     */
    @PutMapping
    @ApiOperation(value = "添加或修改接口")
    public AjaxResult addOrUpdate(@RequestBody Sstype sstype){
        try {
            if(sstype.getId()==null){//添加
                typeService.add(sstype);
            }else{
                typeService.update(sstype);
            }
            return new AjaxResult();
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult(false,"操作失败");
        }
    }
    @PatchMapping
    @ApiOperation(value = "批量删除接口")
    public AjaxResult bathremove(@RequestBody List<Long> ids){
        try {
            typeService.bathremove(ids);
            return new AjaxResult();
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult(false,"删除失败");
//            return AjaxResult.me().setSuccess(false).setMsg("删除失败");
        }
    }
}
